﻿Public Class FViewSharedItemDetails
    Private Shared mActiveForms As New Generic.Dictionary(Of Integer, FViewSharedItemDetails)

    Public Shared Shadows Sub Show(ByVal owner As IWin32Window, ByVal item As Entities.SharedItem)
        If mActiveForms.ContainsKey(item.Id) Then
            mActiveForms(item.Id).Activate()
        Else
            Dim clsView As New FViewSharedItemDetails
            clsView.ShowIt(item)
            mActiveForms.Add(item.Id, clsView)
        End If
    End Sub

    Public Shared Sub CloseAll()
        For i As Integer = mActiveForms.Count - 1 To 0 Step -1
            mActiveForms.Values(i).Close()
        Next
    End Sub

    Private mSharedItem As Entities.SharedItem
    Private mMediaInfo As Entities.MediaInfo

    Private Sub ShowIt(ByVal item As Entities.SharedItem)
        mSharedItem = item

        Me.Text = String.Format("{0} - Local - Shared Item Details - Buddy 2 Buddy", item.Name)
        Select Case item.ItemType
            Case Entities.ItemType.Movies
                Me.Icon = My.Resources.Movie
            Case Entities.ItemType.TVSeries
                Me.Icon = My.Resources.TV
            Case Else
                Me.Icon = Framework.My.Resources.Buddy
        End Select

        UpdateView(item)

        SetupEvents(True)
        MyBase.Show()
    End Sub

    Private Sub UpdateView(ByVal item As Entities.SharedItem)
        Dim aFiles As Generic.List(Of Entities.SharedItemFile)
        aFiles = EntityHandlers.SharedItemsFiles.GetAllBySharedItemId(item.Id)
        Dim clsMI As Entities.MediaInfo = DataStore.MediaInfo.GetBySharedItemId(item.Id)
        SharedItemMediaInfo1.UpdateView(item, aFiles, clsMI)
    End Sub

    Private Sub FViewSharedItems_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        mActiveForms.Remove(mSharedItem.Id)
        SetupEvents(False)
    End Sub
    Private Sub SetupEvents(ByVal startListening As Boolean)
        If startListening Then
            AddHandler EntityHandlers.SharedItems.UpdatedGUI, AddressOf EventHandler_Updated
            AddHandler EntityHandlers.SharedItems.DeletedGUI, AddressOf EventHandler_Deleted
        Else
            RemoveHandler EntityHandlers.SharedItems.UpdatedGUI, AddressOf EventHandler_Updated
            RemoveHandler EntityHandlers.SharedItems.DeletedGUI, AddressOf EventHandler_Deleted
        End If
    End Sub

    Private Sub EventHandler_Updated(ByVal item As Entities.SharedItem)
        If item.Id = mSharedItem.Id Then UpdateView(item)
    End Sub
    Private Sub EventHandler_Deleted(ByVal item As Entities.SharedItem)
        If item.Id = mSharedItem.Id Then Me.Close()
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.Close()
    End Sub


    Private Sub SharedItemMediaInfo1_SetImdbLink(ByVal link As Uri) Handles SharedItemMediaInfo1.SetImdbLink
        btnIMDB.Visible = True
        btnIMDB.Tag = link
        btnIMDB.Text = Nothing
        btnIMDB.Image = Framework.My.Resources.IMDB_small
    End Sub

    Private Sub btnIMDB_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnIMDB.Click
        Try
            Dim uri As Uri = CType(btnIMDB.Tag, Uri)
            System.Diagnostics.Process.Start(uri.ToString)
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub
End Class